class coach {
    constructor(mongoose) {
        this.mongoose = mongoose
        this.Schema = mongoose.Schema
        // AnimeIntro模板
        this.coachIntro = new this.Schema({
            id: Number,
            userId: String,
            name: String,
            type: String,
            min: Number,
            priaseDegree: Number,
            priaseNumber:Number,
            lessonNumber: Number,
            avatar: String,
            isEmpty: Boolean,
        })
        this.modal = this.mongoose.model('Coache', this.coachIntro)
    }

    // @paras1 查询条件 动漫名字数组
    // @paras2 查询结果过滤器
    // 返回查询结果
    coachsFind(body) {
        return this.modal.find().sort({ priaseNumber: 1 }).skip(10 * body.page).limit(10)
    }

    coachFind(userId) {
        return this.modal.find({ userId: userId })
    }

    addCoach(body) {
        var arr = ''
        for (let index = 0; index < 16; index++) {
            arr = arr + parseInt(Math.random() * 10)
        }
        let order = new this.modal({
            id: parseInt(arr),
            userId: body.userId,
            name: body.name,
            type: "减脂 | 塑型",
            min: 0,
            priaseDegree: 0,
            priaseNumber:0,
            lessonNumber: 0,
            avatar: body.avatar,
            isEmpty: true,
        })
        return order.save()
    }


    minUpdate(body) {
        if (body.res[0].min < parseInt(body.price)) {
            return this.modal.updateOne({ userId: body.userId }, { min: parseInt(body.price) })
        } else {
            return this.modal.find({ id: 1 })
        }
    }

    lessonNumberUpdate(userId) {
        return this.modal.updateOne({ userId: userId }, { $inc: { lessonNumber: 1 } })
    }

    priaseDegree(body,coach){
        return this.modal.updateOne({ userId: body.coachId }, { $inc: { priaseNumber: 1,priaseDegree: body.star*20 } })
    }

}

module.exports = coach
